package com.woniuxy.qimengauth.controller;

import com.woniuxy.qimengauth.bean.UserBean;
import com.woniuxy.qimengauth.service.IUserService;
import com.woniuxy.qimengcommons.bean.ReturnMsg;
import com.woniuxy.qimengcommons.enums.RespStatus;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RequestMapping("/user")
@RestController
public class UserController {

    @Resource
    private IUserService userServiceImpl;


    /**
     * 新增
     * @param userBean
     * @return
     */
    @PostMapping(value="/more",produces = MediaType.APPLICATION_JSON_VALUE)
    public ReturnMsg add(UserBean userBean){
        ReturnMsg returnMsg = new ReturnMsg(RespStatus.SUCCESS);
        try{
            //将密码的明文，进行盐值加密
            String pwd = new SimpleHash("MD5", userBean.getPassword(), userBean.getLoginName(), 1024).toString();
            userBean.setPassword(pwd);

            //将数据存储到数据库中
            userServiceImpl.saveUserBean(userBean);
        }catch (Exception e){
            e.printStackTrace();
            returnMsg.setStatus(RespStatus.FAILED);
        }

        return returnMsg;
    }

    /**
     * 新增
     * @param userBean
     * @return
     */
    @PutMapping(value="/{id}",produces = MediaType.APPLICATION_JSON_VALUE)
    public ReturnMsg update(@PathVariable("id") Integer id, UserBean userBean){
        ReturnMsg returnMsg = new ReturnMsg(RespStatus.SUCCESS);
        try{

            System.out.println(id);
            System.out.println(userBean);


        }catch (Exception e){
            returnMsg.setStatus(RespStatus.FAILED);
        }

        return returnMsg;
    }

}
